import szlschiefdata from 'szlschiefdata'
import { CONST_URL } from '@/consts'
const { connect } = szlschiefdata


/**
 * mqtt websocket
 */

const state = {
  mqttClient: null,
}
const mutations = {
  changeMqttClient(state, payload) {
    state.mqttClient = payload
  }

}
const actions = {
   initMqttClient({ commit,state }) {
    return new Promise((resolve, reject) => {
      connect(CONST_URL.MQTT_URL).then(res => {
        commit('changeMqttClient', res)

        state.mqttClient.addListenClose(() => {
          commit('changeMqttClient', null)
        })
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
